import { Ware } from '../core'
import { Context, Options, Template } from './types'
import confirm from './confirm'
import resolve from './resolve'
import load from './load'
import inquire from './inquire'
import setup from './setup'
import prepare from './prepare'
import rename from './rename'
import render from './render'
import emit from './emit'
import install from './install'
import init from './init'
import complete from './complete'

const creator = new Ware<Context>()

creator.use([
    confirm, // 确认当前创建的project是否冲突或者存在
    resolve, // 解析模板路径
    load, // 加载模板中定义的配置,一般定义在模板的根目录index.js中
    inquire, // 项目生成之前的交互
    setup, // 项目生成之前的钩子函数
    prepare,
    rename,
    render,
    emit,
    install,
    init,
    complete
])



export default async (template: string, project: string = '.', options: Options = {}) => {
    if (template == null || template === '') {
        throw new Error('Missing required argument: `template`.')
    }
    // create context
    const context: Context = {
        template,
        project,
        options,
        src: '',
        dest: '',
        config: Object.create(null),
        answers: Object.create(null),
        files: []
    }
    // running creator
    await creator.run(context)
}

export {
    Options,
    Context,
    Template
}